package com.tools.app.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

/**
 * 获取AndroidMani...xml源数据
 * Created by Administrator on 2016/8/8.
 */
public class MetaUtils {
    /**
     * 获取元数据
     *
     * @param context
     * @param name
     * @return
     */
    public static int getInteger(Context context, String name) {
        try {
            String packageName = context.getPackageName();
            PackageManager packageManager = context.getPackageManager();
            ApplicationInfo ai = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
            Bundle metaData = ai.metaData;
            if (metaData != null) {
                return metaData.getInt(name);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取mainfest中的string 元数据
     *
     * @param context
     * @param name
     * @return
     */
    public static String getString(Context context, String name) {
        try {
            String packageName = context.getPackageName();
            PackageManager packageManager = context.getPackageManager();
            ApplicationInfo ai = packageManager.getApplicationInfo(packageName,
                    PackageManager.GET_META_DATA);
            Bundle metaData = ai.metaData;
            if (metaData != null) {
                return metaData.getString(name);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
